import 'package:flutter/material.dart';
import 'package:yuan_hang_app/base/custom_appbar.dart';
import 'package:yuan_hang_app/http/api.dart';
import 'package:yuan_hang_app/http/http_utils.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/res/w_dimens.dart';
import 'package:yuan_hang_app/utils/router_helper.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/views/report/core_report_page.dart';
import 'package:yuan_hang_app/views/report/leads_source_report_page.dart';
import 'package:yuan_hang_app/views/report/operation_report_page.dart';
import 'package:yuan_hang_app/views/report/sale_funnel_page.dart';
import 'package:yuan_hang_app/views/report/sales_report_page.dart';
import 'package:yuan_hang_app/views/report/salesman_report_page.dart';
import 'package:yuan_hang_app/views/report/widget/empty_widget.dart';
import 'package:yuan_hang_app/widget/w_text.dart';
import 'package:yuan_hang_app/widget/w_toast.dart';

import '../../bean/user.dart';
import '../../res/w_constant.dart';

class ReportPage extends StatefulWidget {
  const ReportPage({super.key});

  @override
  State<ReportPage> createState() => _ReportPageState();
}

class _ReportPageState extends State<ReportPage> {
  User user = WConstant().userInfo;

  List<String> reportNameList = [];

  @override
  void initState() {
    super.initState();
    fetchReportList();
  }

  void fetchReportList() {
    reportNameList = [];
    HttpUtils()
        .get("${Api.getReportAuthList}?empId=${WConstant().userInfo.empId}")
        .then((value) {
      if (value.resultCode == 200) {
        List tempList = value.data ?? [];
        for (int i = 0; i < tempList.length; i++) {
          reportNameList.add(tempList[i]["MENU_NAME"]);
        }
        setState(() {});
      } else {
        Toast.toast(value.errMsg);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        CustomAppBar(
          leftWidget: const SizedBox(),
          contentTitle: "报表",
        ),
        if (reportNameList.isNotEmpty)
          Expanded(
              child: ListView.builder(
            padding: EdgeInsets.zero,
            itemBuilder: _saleItem,
            itemCount: reportNameList.length,
          )),
        if (reportNameList.isEmpty)
          EmptyWidget(onTap: (){
            fetchReportList();
          },)
      ],
    );
  }

  Widget _saleItem(context, index) {
    String label = reportNameList[index];
    return GestureDetector(
      onTap: () {
        if (label == "核心四率报表") {
          RouteHelper.pushWidget(context, const CoreReportPage());
        } else if (label == "线索运营报表") {
          RouteHelper.pushWidget(context, const OperationReportPage());
        } else if (label == "线索来源渠道分布报表") {
          RouteHelper.pushWidget(context, const LeadsSourceReportPage());
        } else if (label == "销售占比分布报表") {
          RouteHelper.pushWidget(context, const SalesReportPage());
        } else if (label == "销售顾问业绩统计报表") {
          RouteHelper.pushWidget(context, const SalesmanReportPage());
        }
      },
      child: Container(
        margin: EdgeInsets.only(top: 15.px, left: 15.px, right: 15.px),
        padding: EdgeInsets.symmetric(horizontal: 15.px),
        height: 60.px,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: WColors.primaryColor.withOpacity(0.6),
            borderRadius: BorderRadius.circular(10.px)),
        child: Row(
          children: [
            Image.asset(
              "images/ic_report.png",
              width: 28.px,
              color: WColors.white,
            ),
            SizedBox(
              width: 10.px,
            ),
            Expanded(
                child: WText(
              label,
              style: TextStyle(
                  color: WColors.white,
                  fontSize: WDimens.tsLarge,
                  fontWeight: FontWeight.bold),
            )),
            Image.asset(
              "images/ic_arrow_circle.png",
              width: 20.px,
            ),
          ],
        ),
      ),
    );
  }
}
